import defineProperty from './defineProperty';
import {def} from './utils';
import {arrayMethods} from './array';
import observe from "./observe";
import Dep from './Dep';

export default class Observer {
    constructor(value) {
        this.dep = new Dep();

        def(value, '__ob__', this, false);

        if (Array.isArray(value)) {
            Object.setPrototypeOf(value, arrayMethods);
            this.observeArray(value);
        } else {
            this.walk(value);
        }
    }

    walk(value) {
        for (const key in value) {
            defineProperty(value, key);
        }
    }

    observeArray(arr) {
        for (const value of arr) {
            if (value instanceof Object) {
                observe(value);
            }
        }
    }
}